﻿/**
* @authors <darl2ng@gmail.com, ngocanh.ngo@gmail.com>
* @version 0.9
* Copyright 2010
* 
* The MIT License
* Copyright (c) 2010 <darl2ng@gmail.com, ngocanh.ngo@gmail.com>
* 
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* 
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
* 
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package  com.ng.ResHelp {
	
	import flash.display.Loader;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.ProgressEvent;
	import flash.net.URLRequest;

	public class c_Loader	{
		
		private var __ID:String;
		private var __Loader:Loader;
		private var __LoadingChannel:c_LoadingChannel;		
		private var __IsLoaded:Boolean;
		private var __IsStarted:Boolean;
		private var __Error:Boolean;

		public function c_Loader(p_ID:String, p_Path:String, p_LoadingChannel:c_LoadingChannel) {
			__ID = p_ID;			
			__IsLoaded = false;
			__IsStarted = false;
			__Error = false;
			
			__Loader = new Loader();
			__Loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, OnProgress);
			__Loader.contentLoaderInfo.addEventListener(Event.COMPLETE, OnLoaded);
			__Loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, OnError);
			__Loader.load(new URLRequest(p_Path));
			
			__LoadingChannel = p_LoadingChannel;
			__LoadingChannel.register(this);							
		}
		
		public function get id():String {
			return __ID;
		}
		public function get bytesLoaded():uint {
			return __Loader.contentLoaderInfo.bytesLoaded;
		}
		public function get bytesTotal():uint {
			return __Loader.contentLoaderInfo.bytesTotal;
		}
		public function get isError():Boolean {
			return __Error;
		}
		public function get isLoaded():Boolean {
			return __IsLoaded;
		}
		public function get isStarted():Boolean {
			return __IsStarted;
		}
		public function getClassById(p_ID:String):Class {				
			try {				
				var cl:Class;
				cl = __Loader.contentLoaderInfo.applicationDomain.getDefinition(p_ID);
				return cl;
			} catch (e:ReferenceError) {
				throw new Error("c_Loader::ClassNotExist[" + p_ID +"]");
			}
		}
		
		private function OnProgress(event:Event):void {
			__IsStarted = true;
			__LoadingChannel.OnElementProgress(this);
		}
		private function OnLoaded(event:Event):void {
			__IsLoaded = true;			
			__LoadingChannel.OnElementLoaded(this);
			cleanUp();
		}
		private function OnError(event:Event):void {
			__IsLoaded = false;
			__Error = true;
			cleanUp();
			throw new Error("c_Loader::RessourceNotAvailable[" + __ID +"]");
		}	
		private function cleanUp():void {
			__Loader.contentLoaderInfo.removeEventListener(ProgressEvent.PROGRESS, OnProgress);
			__Loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, OnLoaded);
			__Loader.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR, OnError);
		}
	}

}